package com.hb.guli.service.edu.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.hb.guli.service.edu.entity.Chapter;
import com.hb.guli.service.edu.entity.Video;
import com.hb.guli.service.edu.entity.vo.ChapterVo;
import com.hb.guli.service.edu.entity.vo.VideoVo;
import com.hb.guli.service.edu.feign.VodMediaService;
import com.hb.guli.service.edu.mapper.ChapterMapper;
import com.hb.guli.service.edu.mapper.VideoMapper;
import com.hb.guli.service.edu.service.ChapterService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * <p>
 * 课程 服务实现类
 * </p>
 *
 * @author jack
 * @since 2023-06-30
 */
@Service
public class ChapterServiceImpl extends ServiceImpl<ChapterMapper, Chapter> implements ChapterService {

    @Autowired
    private VideoMapper videoMapper;

    @Autowired
    private VodMediaService vodMediaService;

    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean removeChapterById(String id) {
        // 跟据 courseId 删除 Video
        final QueryWrapper<Video> videoQueryWrapper = new QueryWrapper<>();
        videoQueryWrapper.eq("chapter_id", id);
        final List<Video> videos = videoMapper.selectList(videoQueryWrapper);
        final List<String> collect = videos.stream().map(Video::getVideoSourceId)
                .filter(Objects::nonNull).collect(Collectors.toList());
        vodMediaService.removeVideoByIdList(collect);
        final List<String> ids = videos.stream().map(Video::getId).collect(Collectors.toList());
        videoMapper.deleteBatchIds(ids);

        return this.removeById(id);
    }

    @Override
    public List<ChapterVo> nestedList(String courseId) {
        // 组装章节列表
        // 获取章节列表信息
        final QueryWrapper<Chapter> chapterQueryWrapper = new QueryWrapper<>();
        chapterQueryWrapper.eq("course_id", courseId);
        chapterQueryWrapper.orderByAsc("sort", "id");
        final List<Chapter> chapters = baseMapper.selectList(chapterQueryWrapper);

        // 课时信息
        final QueryWrapper<Video> videoQueryWrapper = new QueryWrapper<>();
        videoQueryWrapper.eq("course_id", courseId);
        videoQueryWrapper.orderByAsc("sort", "id");
        final List<Video> videos = videoMapper.selectList(videoQueryWrapper);

        // 遍历 list
        final ArrayList<ChapterVo> chapterVoList = new ArrayList<>();
        for (Chapter chapter : chapters) {
            final ChapterVo chapterVo = new ChapterVo();
            BeanUtils.copyProperties(chapter, chapterVo);

            final ArrayList<VideoVo> videoVoList = new ArrayList<>();
            for (Video video : videos) {
                if (video != null && chapter.getId().equals(video.getChapterId())) {
                    final VideoVo videoVo = new VideoVo();
                    BeanUtils.copyProperties(video, videoVo);
                    videoVoList.add(videoVo);
                }
            }
            chapterVo.setChildren(videoVoList);

            chapterVoList.add(chapterVo);
        }

        return chapterVoList;
    }
}
